package Admin;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JDesktopPane;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;

import com.example.finalproj.ChatData;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.File;
import java.io.FileOutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.awt.Font;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.Toolkit;
import java.awt.Color;



public class Management extends JFrame {
	
	HashMap<String, Chat_Internal_Frame> design;
	Chat_Server_Log csl;
	
	JDesktopPane main_jdp;
	DB_Server_Log db_log;
	
	private JPanel contentPane;
	private JMenuBar menuBar;
	private JMenu menu;
	private JMenuItem mntmNewMenuItem_1;
	private JMenu mnNewMenu_1;
	private JMenuItem menuItem_4;
	private LoginPanel login;
	private JMenuItem mntmNewMenuItem_2;
	private JMenu mnNewMenu;
	private JMenuItem mntmNewMenuItem_4;
	private JMenu mnNewMenu_2;
	public JMenuItem mntmNewMenuItem;
	
	
	public boolean loginstate = false;
	public JMenuItem mntmNewMenuItem_3;
	private JMenu mnNewMenu_3;
	private JMenuItem mntmNewMenuItem_5;
	private JMenuItem mntmNewMenuItem_6;
	private JMenu mnNewMenu_4;
	private JMenuItem mntmNewMenuItem_7;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Management frame = new Management();
					
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
	

	/**
	 * Create the frame.
	 */
	public Management() {
		setTitle("Final Salon Management");
		setForeground(Color.BLACK);
		setBackground(Color.WHITE);
		setIconImage(Toolkit.getDefaultToolkit().getImage(Management.class.getResource("/com/sun/java/swing/plaf/windows/icons/Computer.gif")));
		addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent arg0) {
				try
				{
					Iterator<String> chat_user = csl.user.keySet().iterator();
					while(chat_user.hasNext())
					{
						ServerThread st = csl.user.get(chat_user.next());
						ChatData cd = new ChatData();
						cd.setAction(3);
						st.oos.writeObject(cd);
						st.flag=false;
					}
					Iterator<String> inter_design = design.keySet().iterator();
					while(inter_design.hasNext())
					{
						Chat_Internal_Frame inter = design.get(inter_design.next());
						Iterator<String> chatting = inter.talk.keySet().iterator();
						while(chatting.hasNext())
						{
							Chatting ch = inter.talk.get(chatting.next());
							ch.doDefaultCloseAction();
							ch.dispose();
						}
						inter.dispose();
					}
					
					
					csl.savelog();
					csl.flag = false;
//					csl.server.close();
//					System.out.println(InetAddress.getLocalHost().getHostAddress());
//					new Socket(InetAddress.getLocalHost().getHostAddress(), 8282).close();
					csl.dispose();
					
					db_log.savelog();
					db_log.flag = false;
//					db_log.db_server.close();
					db_log = null;
				}
				catch(Exception ex)
				{
					ex.printStackTrace();
				}
//					System.out.println("끄진다공");
			}
		});
		db_log = new DB_Server_Log();
		design = new HashMap<String, Chat_Internal_Frame>();
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(0, 0, 867, 685);
		setJMenuBar(getMenuBar_1());
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);
		login = new LoginPanel(contentPane,this);
		
		main_jdp = new JDesktopPane();
		csl = new Chat_Server_Log(main_jdp, design);
		main_jdp.add(csl);
		csl.setVisible(true);
		
		contentPane.removeAll();
		contentPane.add(login);
		contentPane.updateUI();
	}
	


	private JMenuBar getMenuBar_1() {
		if (menuBar == null) {
			menuBar = new JMenuBar();
			menuBar.add(getMenu());
			menuBar.add(getMnNewMenu());
			menuBar.add(getMnNewMenu_2());
			menuBar.add(getMnNewMenu_3());
			menuBar.add(getMnNewMenu_1());
			menuBar.add(getMnNewMenu_4());
		}
		return menuBar;
	}
	private JMenu getMenu() {
		if (menu == null) {
			menu = new JMenu("회원관리");
			menu.setFont(new Font("Malgun Gothic", Font.PLAIN, 12));
			menu.add(getMntmNewMenuItem_1());
			menu.add(getMntmNewMenuItem_2());
			menu.add(getMntmNewMenuItem_3());
		}
		return menu;
	}
	private JMenuItem getMntmNewMenuItem_1() {
		if (mntmNewMenuItem_1 == null) {
			mntmNewMenuItem_1 = new JMenuItem("회원관리");
			mntmNewMenuItem_1.setFont(new Font("Malgun Gothic", Font.PLAIN, 12));
			mntmNewMenuItem_1.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					if(loginstate){
						contentPane.removeAll();
						contentPane.add(new MemberPanel(contentPane));
						contentPane.updateUI();
					}else{
						contentPane.removeAll();
						contentPane.add(login);
						contentPane.updateUI();
					}
				
				}
			});
		}
		return mntmNewMenuItem_1;
	}
	private JMenu getMnNewMenu_1() {
		if (mnNewMenu_1 == null) {
			mnNewMenu_1 = new JMenu("채팅");
			mnNewMenu_1.setFont(new Font("Malgun Gothic", Font.PLAIN, 12));
			mnNewMenu_1.add(getMenuItem_4());
			mnNewMenu_1.add(getMntmNewMenuItem_6());
		}
		return mnNewMenu_1;
	}
	private JMenuItem getMenuItem_4() {
		if (menuItem_4 == null) {
			menuItem_4 = new JMenuItem("채팅 보기");
			menuItem_4.setFont(new Font("Malgun Gothic", Font.PLAIN, 12));
			menuItem_4.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					contentPane.removeAll();
					contentPane.add(main_jdp);
					contentPane.updateUI();

					
//					Chat_Internal_Frame cif = new Chat_Internal_Frame(Management.this, chat_list);
//					jdp.add(cif);
//					cif.setVisible(true);
				}
			});
		}
		return menuItem_4;
	}
	private JMenuItem getMntmNewMenuItem_2() {
		if (mntmNewMenuItem_2 == null) {
			mntmNewMenuItem_2 = new JMenuItem("회원가입");
			mntmNewMenuItem_2.setFont(new Font("Malgun Gothic", Font.PLAIN, 12));
			mntmNewMenuItem_2.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					if(loginstate){
					contentPane.removeAll();
					contentPane.add(new MemberJoin(contentPane));
					contentPane.updateUI();
					}else{
						contentPane.removeAll();
						contentPane.add(login);
						contentPane.updateUI();
					}
				
				}
			});
		}
		return mntmNewMenuItem_2;
	}
	private JMenu getMnNewMenu() {
		if (mnNewMenu == null) {
			mnNewMenu = new JMenu("아이템 관리");
			mnNewMenu.setFont(new Font("Malgun Gothic", Font.PLAIN, 12));
			mnNewMenu.add(getMntmNewMenuItem_4());
		}
		return mnNewMenu;
	}
	private JMenuItem getMntmNewMenuItem_4() {
		if (mntmNewMenuItem_4 == null) {
			mntmNewMenuItem_4 = new JMenuItem("헤어스타일 관리");
			mntmNewMenuItem_4.setFont(new Font("Malgun Gothic", Font.PLAIN, 12));
			mntmNewMenuItem_4.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					if(loginstate){
						contentPane.removeAll();
						contentPane.add(new Style(contentPane));
						contentPane.updateUI();
						
					}else{
						contentPane.removeAll();
						contentPane.add(login);
						contentPane.updateUI();
					}
					
					
				}
			});
		}
		return mntmNewMenuItem_4;
	}
	private JMenu getMnNewMenu_2() {
		if (mnNewMenu_2 == null) {
			mnNewMenu_2 = new JMenu("예약 관리");
			mnNewMenu_2.setFont(new Font("맑은 고딕", Font.PLAIN, 12));
			mnNewMenu_2.add(getMntmNewMenuItem());
		}
		return mnNewMenu_2;
	}
	private JMenuItem getMntmNewMenuItem() {
		if (mntmNewMenuItem == null) {
			mntmNewMenuItem = new JMenuItem("예약확인");
			mntmNewMenuItem.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					
					if(loginstate){
						contentPane.removeAll();
						contentPane.add(new reservation(contentPane));
						contentPane.updateUI();
						
					}else{
						contentPane.removeAll();
						contentPane.add(login);
						contentPane.updateUI();
					}
					
					
					
				}
			});
			mntmNewMenuItem.setFont(new Font("맑은 고딕", Font.PLAIN, 12));
		}
		return mntmNewMenuItem;
	}
	private JMenuItem getMntmNewMenuItem_3() {
		if (mntmNewMenuItem_3 == null) {
			mntmNewMenuItem_3 = new JMenuItem("Log In");
			mntmNewMenuItem_3.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					loginstate = false;
					
					contentPane.removeAll();
					contentPane.add(login);
					contentPane.updateUI();
					mntmNewMenuItem_3.setText("Log In");
					
				}
			});
			mntmNewMenuItem_3.setFont(new Font("Malgun Gothic", Font.PLAIN, 12));
		}
		return mntmNewMenuItem_3;
	}
	private JMenu getMnNewMenu_3() {
		if (mnNewMenu_3 == null) {
			mnNewMenu_3 = new JMenu("Q&A");
			mnNewMenu_3.setFont(new Font("Malgun Gothic", Font.PLAIN, 12));
			mnNewMenu_3.add(getMntmNewMenuItem_5());
		}
		return mnNewMenu_3;
	}
	private JMenuItem getMntmNewMenuItem_5() {
		if (mntmNewMenuItem_5 == null) {
			mntmNewMenuItem_5 = new JMenuItem("Q&A");
			mntmNewMenuItem_5.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					
					if(loginstate){
						contentPane.removeAll();
						contentPane.add(new QnA(contentPane));
						contentPane.updateUI();
						
					}else{
						contentPane.removeAll();
						contentPane.add(login);
						contentPane.updateUI();
					}
					
				}
			});
			mntmNewMenuItem_5.setFont(new Font("Malgun Gothic", Font.PLAIN, 12));
		}
		return mntmNewMenuItem_5;
	}
	
	public void addInternal()
	{
		
	}
	public JMenuItem getMntmNewMenuItem_6() {
		if (mntmNewMenuItem_6 == null) {
			mntmNewMenuItem_6 = new JMenuItem("디자이너 로그인");
			mntmNewMenuItem_6.setFont(new Font("Malgun Gothic", Font.PLAIN, 12));
			mntmNewMenuItem_6.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					Chat_Internal_Frame ci = new Chat_Internal_Frame(csl.user, csl.ta_log, design);
					main_jdp.add(ci);
					ci.setVisible(true);
				}
			});
		}
		return mntmNewMenuItem_6;
	}
	public JMenu getMnNewMenu_4() {
		if (mnNewMenu_4 == null) {
			mnNewMenu_4 = new JMenu("DB 로그");
			mnNewMenu_4.setFont(new Font("Malgun Gothic", Font.PLAIN, 12));
			mnNewMenu_4.add(getMntmNewMenuItem_7());
		}
		return mnNewMenu_4;
	}
	public JMenuItem getMntmNewMenuItem_7() {
		if (mntmNewMenuItem_7 == null) {
			mntmNewMenuItem_7 = new JMenuItem("DB 접속이력");
			mntmNewMenuItem_7.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					contentPane.removeAll();
					contentPane.add(db_log);
					contentPane.updateUI();
				}
			});
		}
		return mntmNewMenuItem_7;
	}
}
